package com.whately.core.base.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.ToString;

import java.io.Serializable;

/**
 * 分页请求vo
 */
@ApiModel(value = "分页请求")
@ToString
public class PageRequestBO implements Serializable {
    private static final long serialVersionUID = -4026346032261456086L;

    @ApiModelProperty(value = "请求页码",required = true,example = "1",position = 99999)
    private int page = 1;
    @ApiModelProperty(value = "单页数量",required = true,example = "10",position = 88888)
    private int size = 10;

    @ApiModelProperty(hidden = true)
    private int start = 0;
    @ApiModelProperty(hidden = true)
    private Integer rows = Integer.valueOf(10);

    public PageRequestBO() {
    }

    public int getStart() {
        if(this.page > 0 && this.size > 0) {
            this.start = (this.page - 1) * this.size;
        } else {
            this.start = 0;
        }

        return this.start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getPage() {
        return this.page;
    }

    public void setPage(int page) {
        if(page <= 1) {
            page = 1;
        }

        this.page = page;
    }

    public int getSize() {
        return this.size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void checkCurrentPage(int count) {
        int totalPage = count % this.size == 0?count / this.size:count / this.size + 1;
        if(this.page > totalPage) {
            this.page = Math.max(totalPage, 1);
        }

    }

    public Integer getRows() {
        return this.rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }


    public static void main(String[] args) {
        PageRequestBO p=new PageRequestBO();
        p.setSize(300);
        System.out.println(p.toString());
    }
}
